<template>
  <div>
    <!-- 列表 -->
    <ul class="TemplateStyle">
      <!-- 生产日期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">生产日期</span>
        </div>
        <div class="label">
          <input type="text" :value="userDate" :disabled="true" />
        </div>
      </li>
      <!-- 生产物资(名称) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/birth.png" />
          <span class="li_txt">生产物资(名称)</span>
        </div>
        <div class="label">
          <input type="text" v-model="produceName" />
        </div>
      </li>
      <!-- 生产物资(单位) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/order.png" />
          <span class="li_txt">生产物资(单位)</span>
        </div>
        <div class="label">
          <input type="text" v-model="produceUnit" />
        </div>
      </li>
      <!-- 生产物资(数量) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/money.png" />
          <span class="li_txt">生产物资(数量)</span>
        </div>
        <div class="label">
          <input type="number" v-model="produceNum" />
        </div>
      </li>
      <!-- 预计生产周期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/term.png" />
          <span class="li_txt">预计生产周期(天)</span>
        </div>
        <div class="label">
          <input type="number" v-model="producePeriod" />
        </div>
      </li>
      <!-- 确认按钮 -->
      <button class="btn" @click="btnPlan">确定</button>
    </ul>
    <!-- loading -->
    <div style="padding-bottom: 4rem; position:top:50%">
      <van-loading
        v-show="loading"
        style="position: absolute;top: 50%;left: 46%;"
        type="spinner"
        color="#1989fa"
      ></van-loading>
    </div>
  </div>
</template>
<script>
import moment from "moment";

export default {
  name: "ProductionPlan",
  data() {
    return {
      userDate: "", // 展示给用户
      produceTime: "",
      produceName: "",
      produceUnit: "",
      produceNum: "",
      producePeriod: "",
      loading: false // loading
    };
  },
  created() {
    // 自动生成日期
    let nowDate = new Date();
    this.produceTime = moment(nowDate).format("YYYY-MM-DD HH:mm:ss");
    this.userDate = moment(nowDate).format("YYYY-MM-DD");
  },
  methods: {
    btnPlan() {
      this.loading = true; // loading显示
      let params = {
        token: JSON.parse(localStorage.getItem("login")).res.token,
        userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
        produceTime: this.produceTime,
        produceName: this.produceName,
        produceUnit: this.produceUnit,
        produceNum: this.produceNum,
        producePeriod: this.producePeriod
      };
      this.$api
        .requestPlan(params)
        .then(res => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: res
          });
        })
        .catch(err => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: err
          });
        });
    }
  }
};
</script>
<style>
@import "../../assets/css/TemplateStyle.css";
</style>
